<template>
  <div class="auth-page">
    <div class="auth-page-text">
      <span v-if="!hasError">微信授权登陆中...</span>
      <span v-else>微信授权登陆失败</span>
    </div>
  </div>
</template>

<script>
  import CommonApi from 'services/commonApi';

  import storeMixins from 'mixins/storeMixins';

  export default {
    mixins: [storeMixins],

    data() {
      let data = this.$route.query;
      return {
        hasError: false,
        code: data.code,
        url: data.url
      };
    },

    created() {
      this.authLogin();
    },

    methods: {
      authLogin() {
        let code = this.code;
        CommonApi.authLogin(code).then(res => {
          let url = this.url;
          if (url === '/systemIndex') {
            url = '/home'
          }
          let user = res.user;
          if (SystemUtil.isEmpty(user.phoneNum)) {
            user.phoneNum = '';
          }
          this.setUser(user);
          this.setDefaultUser(null);

          this.setAuthToken(res.authToken);
          this.setRefreshToken({
            data: res.refreshToken,
            time: this.$moment().valueOf()
          });

          this.hasError = false;
          this.$router.push({
            path: url
          });
        }).catch(() => {
          this.hasError = true;
        });
      }
    }
  };

</script>

<style lang="scss">
  .auth-page {
    height: 100%;
    position: relative;
    font-size: formatPx(32);

    .auth-page-text {
      @extend .center-middle;
    }
  }

</style>
